// pages/order/comment.js
import {
  hotel,
  store,
  gourmet
} from '../../api/apiData'
const upLoadUrl = getApp().globalData.upLoadUrl

Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: 3,
    order_id: '',
    score_location: '',
    score_facility: '',
    score_service: '',
    score_room: '',
    images: '',
    fileList: [],
    fileList2: [],
    content: '',
    type: 1,
    score_delivery: '',
    score_service: '',
    items: [],
    score_taste:'',
    score_environment:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      order_id: options.id
    })
    if (options.type) {
      this.setData({
        type: options.type
      })
    }
    this.getDetail()
  },
  getDetail() {
    if (this.data.type == 1) {
      hotel.order.detail({
        order_id: this.data.order_id,
      }, res => {
        if (res.code == 1) {
          this.setData({
            info: res.data,
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      })
    } else if(this.data.type==2) {
      store.order.detail({
        order_id: this.data.order_id,
      }, res => {
        if (res.code == 1) {
          res.data.items.forEach(item => {
            item.score_product = 0
            item.content = ''
            item.images = ''
            item.fileList=[]
            item.fileList2=[]
          })
          this.setData({
            info: res.data,
            items: res.data.items
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      })
    }else{
      gourmet.order.detail({
        order_id: this.data.order_id,
      }, res => {
        if (res.code == 1) {
          res.data.items.forEach(item => {
            item.score_product = 0
            item.content = ''
            item.images = ''
            item.fileList=[]
            item.fileList2=[]
          })
          this.setData({
            info: res.data,
            items: res.data.items
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'error'
          })
        }
      })
    }

  },
  // 上传图片
  afterRead(event) {
    let that = this
    const {
      file
    } = event.detail;
    console.log(file)
    file.forEach(item => {
      wx.uploadFile({
        url: upLoadUrl, // 仅为示例，非真实的接口地址
        filePath: item.url,
        name: 'file',
        formData: {
          user: 'test'
        },
        success(res) {
          console.log(res)
          const {
            fileList = [],
              fileList2 = []
          } = that.data;
          fileList.push({
            url: JSON.parse(res.data).data.url
          });
          fileList2.push(JSON.parse(res.data).data.path)
          console.log(fileList2)
          that.setData({
            fileList,
            fileList2,
          });
        },
      });
    })
  },
  deleteImg(event) {
    let index = event.detail.index
    let arr = this.data.fileList
    let arr2 = this.data.fileList2
    arr.splice(index, 1)
    arr2.splice(index, 1)
    this.setData({
      fileList: arr,
      fileList2: arr2
    })
  },
  afterRead2(event) {
    console.log(event)
    let index = event.currentTarget.dataset.index
    let that = this
    const {
      file
    } = event.detail;
    console.log(file)
    file.forEach(item => {
      wx.uploadFile({
        url: upLoadUrl, // 仅为示例，非真实的接口地址
        filePath: item.url,
        name: 'file',
        formData: {
          user: 'test'
        },
        success(res) {
          console.log(res)
          const fileList = that.data.items[index].fileList
          const fileList2 = that.data.items[index].fileList2
          fileList.push({
            url: JSON.parse(res.data).data.url
          });
          fileList2.push(JSON.parse(res.data).data.path)
          console.log(fileList2)
          that.setData({
            ['items['+index+'].fileList']:fileList,
            ['items['+index+'].fileList2']:fileList2,
            ['items['+index+'].images']:fileList2.toString()
          });
        },
      });
    })
  },
  deleteImg2(event) {
    console.log(event)
    let i = event.currentTarget.dataset.index
    let index = event.detail.index
    let arr = this.data.items[i].fileList
    let arr2 = this.data.items[i].fileList2
    arr.splice(index, 1)
    arr2.splice(index, 1)
    this.setData({
      ['items['+i+'].fileList']: arr,
      ['items['+i+'].fileList2']: arr2,
      ['items['+i+'].images']: arr2.toString()
    })
  },
  onChange(event) {
    let name = event.currentTarget.dataset.name
    this.setData({
      [name]: event.detail,
    });
  },
  toSuggest() {
    let obj = {
      order_id: this.data.order_id,
      score_location: this.data.score_location,
      score_facility: this.data.score_facility,
      score_service: this.data.score_service,
      score_room: this.data.score_room,
      content: this.data.content,
      images: this.data.fileList2.toString(),
    }
    if (obj.content == '') {
      wx.showToast({
        title: '请输入建议内容',
        icon: 'error'
      })
      return false
    }
    console.log(obj)
    hotel.comment.submit(obj, res => {
      if (res.code == 1) {
        wx.showToast({
          title: res.msg
        })
        setTimeout(() => {
          wx.reLaunch({
            url: '/pages/my/comment',
          })
        }, 1000)
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    })
  },
  onChange2(event) {
    console.log(event)
    let index = event.currentTarget.dataset.index
    let value = event.detail
    this.setData({
      ['items['+index+'].score_product']: value,
    });
  },
  onInput(e){
    let index = e.currentTarget.dataset.index
    let value = e.detail.value
    this.setData({
      ['items['+index+'].content']:value
    })
  },
  toSuggest2(){
    let list = this.data.items
    let product_list = []
    list.forEach(item=>{
      product_list.push({
        item_id:item.id,
        score_product:item.score_product,
        content:item.content,
        images:item.images,
      })
    })
    let obj = {
      order_id: this.data.order_id,
      score_delivery: this.data.score_delivery,
      score_service: this.data.score_service,
      product_list:product_list
    }
    store.comment.submit(obj, res => {
      if (res.code == 1) {
        wx.showToast({
          title: res.msg
        })
        setTimeout(() => {
          wx.reLaunch({
            url: '/pages/my/comment?active=1',
          })
        }, 1000)
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    })
  },
  toSuggest3() {
    let obj = {
      order_id: this.data.order_id,
      score_taste: this.data.score_taste,
      score_environment: this.data.score_environment,
      score_service: this.data.score_service,
      content: this.data.content,
      images: this.data.fileList2.toString(),
    }
    if (obj.content == '') {
      wx.showToast({
        title: '请输入建议内容',
        icon: 'error'
      })
      return false
    }
    console.log(obj)
    gourmet.comment.submit(obj, res => {
      if (res.code == 1) {
        wx.showToast({
          title: res.msg
        })
        setTimeout(() => {
          wx.reLaunch({
            url: '/pages/my/comment?active=2',
          })
        }, 1000)
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})